package managerSIP;

import java.util.Hashtable;
import java.util.List;
import java.util.ListIterator;

public class AsteriskSession {

    Hashtable<String, String> usersID = new Hashtable<String, String>();

    Hashtable<String, Integer> usersConf = new Hashtable<String, Integer>();

    Hashtable<Integer, List<String>> conferences = new Hashtable<Integer, List<String>>();

    private String sessionName;

    List<String> usersSession;
    //List of users associating a conference number to this users session

    private Manager myManager;

    public AsteriskSession(String name, List<String> usersSession,Manager myManager) {
	this.myManager = myManager;
	this.usersSession = usersSession;
	this.sessionName = name;
	//As we invented this notion of Session... we will treat the notification
	// of the event regarding "a user joined a session" here
	notifyUsersJoinedSession();
    }

    /**
     * Creates a conference among a group of users...
     * @param users String[] List of users to be connected to the same conference.
     * @return int Returns the number of the created conference. If any problem, returns false.
     */

    boolean createConference(List<String> users){
	//Implement conference Creation
	try{
	    int confNumber = generateAndRegisterUniqueConfNumber();
	    System.out.println("(boolean)createConference -- confNumber: " + confNumber + "\n");
	    //Invite all users to the same conference number
	    /* TESTE */
	    System.out.println("TESTE -- entrando no Iterator it1 = users.iterator();");
	    ListIterator<String> it1 = users.listIterator();
	    System.out.println("Esta vazio :" + it1.hasNext());
	    System.out.println("Primeiro elemento: " + it1.next());

	    for (ListIterator<String> it = users.listIterator(); it.hasNext();){
		System.out.println("Dentro laco for");
		String strUsersList = it.next();
		System.out.println("Dentro laco for: "+strUsersList);
		//System.out.println("Entrando no InviteThread: " + strUsersList + confNumber);
		new InviteThread(strUsersList,confNumber,myManager).start();
	    }
	    //Add to the list of conferences of this session
	    this.conferences.put(Integer.valueOf(confNumber),users);
	    return true;

	}catch (Exception e){
	    System.err.println("Erro em AsteriskSession -- CreateConference");
	    e.printStackTrace();
	    return false;
	}

    }

    boolean createConference(List<String> users, int conf){
	//Implement conference Creation
	try{
	    //Invite all users to the same conference argument number
	    for (ListIterator<String> it = users.listIterator(); it.hasNext();){
		String strUsersList = it.next();
		new InviteThread(strUsersList,conf,myManager).start();
	    }
	    //Add to the list of conferences of this session
	    this.conferences.put(conf,users);
	    return true;

	}catch (Exception e){
	    e.printStackTrace();
	    return false;
	}

    }

    //This method generates a number randomly, and registers it into the Manager
    // to guarantee that it is a unique number...
    int generateAndRegisterUniqueConfNumber(){
	java.util.Random generator = new java.util.Random();
	//what kind of number should be a conference number ????
	int newConfNumber = generator.nextInt(100);
	while (!myManager.registerConferenceNumber(newConfNumber,sessionName))
	    newConfNumber = generator.nextInt(100);
	return newConfNumber;
    }

    void notifyUsersJoinedSession(){
	//first we check if there really exist a Listener waiting notification
	if (myManager.managerListener!=null){
	    for (ListIterator<String> it = usersSession.listIterator(); it.hasNext();) {
		String strUsersList = it.next();
		myManager.managerListener.userJoinedSession(sessionName,strUsersList);
	    }
	}

    }
}